import http = require("http")

import { HttpHandleAttr, IHandle } from "./handleManager";

import { ParsedUrlQuery } from "querystring";
import { DBManager } from "../data/dbmanager";
import { Test3 } from "../data/model_test3";
import { Test4 } from "../data/model_test4";

@HttpHandleAttr("/db")
class Handle_DB implements IHandle
{
    async onGet(query: ParsedUrlQuery, res: http.ServerResponse): Promise<void>
    {
        await DBManager.Init();
        if (query["op"] == "write")
        {
            //统一只保留一种使用方式
            let values = {
                username: 'janedoe',
                birthday: new Date(1980, 6, 20)
            }
            let data3 = Test3.build(values);
            data3.username = "alibaba";
            await data3.save();


            var data4 = Test4.build(values);
            data4.nihaoma = "very good";
            await data4.save();

            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.write("this is Handle_DB WriteTest.");
            res.end();
            return;
        }
        if (query["op"] == "read")
        {
            res.writeHead(200, { 'Content-Type': 'text/html' });
            res.write("this is Handle_DB ReadTest.");
            res.end();
            return;
        }
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write("this is Handle_DB Get.");
        res.end();
    }
    async onPost(req: http.IncomingMessage, res: http.ServerResponse): Promise<void>
    {
        throw new Error("this is Handle_DB Post.");
    }
}


